@echo off

rem *************************************************************************
rem ** Directories
rem *************************************************************************
set DIR_SAGEM=D:\SAGEM

rem *************************************************************************
rem ** GNU Directories
rem *************************************************************************
set GNU_VERSION=3.4.3
set GNU_DIR=%DIR_SAGEM%\GCC_%GNU_VERSION%

rem *************************************************************************
rem ** SDK Directories
rem *************************************************************************
set SDK30_VER=5.5
set SDK30_ROOT=%DIR_SAGEM%\SDK30\TDS %SDK30_VER%
set SDK30_DIR=%SDK30_ROOT%\SDK
set SDK30_INC=%SDK30_DIR%\INC
set SDK30_LIB=%SDK30_DIR%\Lib_GNU

rem *************************************************************************
rem ** EMV & InterApp Directories
rem *************************************************************************
set EMV_INC_DIR=..\Inc
set EMV_CUSTOM_SRC_DIR=..\Src
set EMV_LIB_ADS_DIR=..\lib_rvd
set EMV_LIB_GNU_DIR=..\lib_gnu
set EMV_LIB_SDT_DIR=..\lib_sdt
set EMV_LIB_VC6_DIR=..\lib_vc6
set EMV_LIB_VS2005_DIR=\lib_vs2005
set GLOB_INTER=..\..\inc

rem *************************************************************************
rem ** Project & Object Directories
rem *************************************************************************
set GENERIC_SRC_DIR=.\Src
set GENERIC_INC_DIR=.\Inc
set GENERIC_LIB_DIR=.\Lib
set OBJ_DIR=%CD%\Obj

rem *************************************************************************
rem ** Schemes & Output Directories
rem *************************************************************************
set SCHEMES_DIR=%SDK30_ROOT%\Component\Schemes\B1
set OUTPUT_DIR=%CD%\Signed

rem *************************************************************************
rem Application settings
rem *************************************************************************
set APP_NAME=MASTER
set APP_VERSION=35220325

rem !! This is Signed or Mockup Application ?

if(%1)==(MOCKUP) goto GO_MOCKUP

rem *************************************************************************
rem *	Deployment Certification					    *
rem *************************************************************************
set SST_PIN=32826128
set SST_CERTIFICATE_FILE=certif-0002A772-0110-70.V03
goto GO_SCHEME

:GO_MOCKUP
rem *************************************************************************
rem *	Mock Up Certification						    *
rem *************************************************************************
set SST_CERTIFICATE_FILE=Mockup.V03
set SST_PIN=00000000

:GO_SCHEME
set NUMBER_OF_SCHEMES=4
set SCHEME1=SchFree.mot
set SCHEME2=SchGetPin.bin
set SCHEME3=SchIso9564.mot
set SCHEME4=SchLoadData.mot
rem set SCHEME5=SchXOR.mot
rem set SCHEME6=Sch_Random.mot
rem set SCHEME7=SchCipherDa.mot
rem set SCHEME8=SchCipherPR.mot
rem set SCHEME9=SchDukpt.mot
rem set SCHEME10=SchDukpTDes.mot

set OUTPUT_DATA_SIZE=00080000
set OUTPUT_APPLI_TYPE=0051

set CATALOG_FILE=31
set OUTPUT_COMMENT=Master Application
set OUTPUT_ZIPPED=1
set OUTPUT_CHECK_CRC=1
set OUTPUT_FILE_NAME=%APP_VERSION%.SGN
set OUTPUT_FAMILY_NAME=%APP_VERSION%

rem set OUTPUT_FILE_TYPE=1
rem set OUTPUT_CRC=FFFF
rem set OUTPUT_UNZIPPED_SIZE=30000
rem set OUTPUT_ZIPPED_SIZE=0
rem set OUTPUT_CODE_ADDRESS=0
rem set OUTPUT_DATA_ADDRESS=200000

set SIGN_APP=0

rem *************************************************************************
rem ** Compiler options
rem *************************************************************************
set USING_TCPIP=0
if .%USING_TCPIP%.==.1. set CC_DEFINES=-D_EXPORT_ -D_SMT_TCP_IP -D_MINI_ATM_
if .%USING_TCPIP%.==.0. set CC_DEFINES=-D_EXPORT_ -D__MAM_SSP__ -D__TEST_SEC_DIAL__ -D__TEST_APP_DOWNLOAD__ -D_MINI_ATM_
set CC_INCLUDES_DIR=-I"%SDK30_INC%" -I"%SDK30_ISO%" -I ".\inc" -I "%GENERIC_INC_DIR%" -I ".\..\.."
set CC_FLAGS=-Os -Wall -Wcast-align -Wsign-compare -mthumb -mthumb-interwork -mlittle-endian -msoft-float -pipe -mcpu=arm920t -fomit-frame-pointer -fshort-enums -mstructure-size-boundary=8 -D_EFT30_ -c
set CC_ERROR=0

rem *************************************************************************
rem ** Linker options
rem *************************************************************************

set LD_OBJECTS1="%OBJ_DIR%\CallServices.o" "%OBJ_DIR%\AppRegister.o"
set LD_OBJECTS2="%OBJ_DIR%\Main.o" "%OBJ_DIR%\Entry.o" "%OBJ_DIR%\hmiDisplay.o" "%OBJ_DIR%\AppData.o" "%OBJ_DIR%\hmiKbd.o" "%OBJ_DIR%\hmiBuzzer.o" "%OBJ_DIR%\Smc.o" "%OBJ_DIR%\hmiPrinter.o" 

set LD_OBJECTS=%LD_OBJECTS1% %LD_OBJECTS2%

rem set LD_OBJECTS=%LD_OBJECTS2%

set AXF_FILE=%OBJ_DIR%\%APP_NAME%.axf
set MAP_FILE=%OBJ_DIR%\%APP_NAME%.map
set BIN_FILE_NAME=%APP_NAME%.bin
set TXT_FILE_NAME=%APP_NAME%.txt

set SGN_FILE_NAME=%APP_NAME%.SGN
set ADF_FILE_NAME=%APP_NAME%.ADF
set M31_FILE_NAME=%APP_NAME%.M31

set BIN_FILE=%OBJ_DIR%\%BIN_FILE_NAME%
set TXT_FILE=%OBJ_DIR%\%TXT_FILE_NAME%

set SGN_FILE=%OBJ_DIR%\%SGN_FILE_NAME%
set ADF_FILE=%OBJ_DIR%\%ADF_FILE_NAME%
set M31_FILE=%OBJ_DIR%\%M31_FILE_NAME%

set LD_FLAGS=-O3 --cref -Map "%MAP_FILE%" -T "%SDK30_DIR%\lib_gnu\gcc_link.txt"
set LD_SYS_OBJECTS="%SDK30_DIR%\lib_gnu\cstartup.o" "%SDK30_LIB%\LinkLayerExeInterface.lib"
set LD_GNU_LIBS=-L"%GNU_DIR%\arm-elf\lib\thumb\soft\interwork" -L"%GNU_DIR%\lib\gcc\arm-elf\%GNU_VERSION%\thumb\soft\interwork" -lc -lgcc

if .%USING_TCPIP%.==.0. set LD_SYS_LIBS="%SDK30_LIB%\eft30.lib" "%SDK30_LIB%\trap.lib"
set LD_ERROR=0
set FROMELF_ERROR=0

rem *************************************************************************
rem ** SST options
rem *************************************************************************
set SST_DIR=%DIR_SAGEM%\TELIUM Tools\SST30 V04.02
set SST_CONFIG_FILE=%SST_DIR%\%APP_NAME%.prm

rem *************************************************************************
rem ** Create output directories
rem *************************************************************************
set ERRORS_FILE=Errors.txt

del "%ERRORS_FILE%" > nul 2> nul

rem rd /s /q "%OBJ_DIR%" > nul 2> nul
md "%OBJ_DIR%" 2>> "%ERRORS_FILE%"
del /q "%OBJ_DIR%\*.*"2>> "%ERRORS_FILE%"

rem rd /s /q "%OUTPUT_DIR%" > nul 2> nul
md "%OUTPUT_DIR%" 2>> "%ERRORS_FILE%"
del /q "%OUTPUT_DIR%\*.*"2>> "%ERRORS_FILE%"

echo %OUTPUT_FILE_TYPE%,%OUTPUT_FILE_NAME%,%OUTPUT_APPLI_TYPE%,%OUTPUT_FAMILY_NAME%,%OUTPUT_ZIPPED%,%OUTPUT_CHECK_CRC%,%OUTPUT_CRC%,%OUTPUT_UNZIPPED_SIZE%,%OUTPUT_ZIPPED_SIZE%,%OUTPUT_DATA_SIZE%,%OUTPUT_CODE_ADDRESS%,%OUTPUT_DATA_ADDRESS%,%OUTPUT_COMMENT%> "%TXT_FILE%"

rem *************************************************************************
rem ** Compile files
rem *************************************************************************
echo Compiling...
echo Compiling... >> "%ERRORS_FILE%"

call :CallCompiler %GENERIC_SRC_DIR% Main
call :CallCompiler %GENERIC_SRC_DIR% Entry
call :CallCompiler %GENERIC_SRC_DIR% hmiDisplay
call :CallCompiler %GENERIC_SRC_DIR% hmiKbd
call :CallCompiler %GENERIC_SRC_DIR% hmiBuzzer
call :CallCompiler %GENERIC_SRC_DIR% AppData
call :CallCompiler %GENERIC_SRC_DIR% Smc
call :CallCompiler %GENERIC_SRC_DIR% hmiPrinter

if .%CC_ERROR%.==.0. goto AfterCompile
echo.
echo.
echo *************************************
echo * THERE ARE COMPILATION ERRORS !!!! *
echo *************************************
pause
goto End

:AfterCompile

rem *************************************************************************
rem ** Link files
rem *************************************************************************
echo Linking...
echo Linking... >> "%ERRORS_FILE%"
call :CallLinker
if .%LD_ERROR%.==.0. goto AfterLink
echo.
echo.
echo ********************************
echo * THERE ARE LINKER ERRORS !!!! *
echo ********************************
pause
goto End

:AfterLink

rem *************************************************************************
rem ** Generate binary file
rem *************************************************************************
echo Generating binary file...
echo Generating binary file... >> "%ERRORS_FILE%"
call :CallFromElf
if .%FROMELF_ERROR%.==.0. goto AfterFromElf
echo.
echo.
echo ************************************************
echo * THERE ARE BINARY FILE GENERATION ERRORS !!!! *
echo ************************************************
pause
goto End

:AfterFromElf
rem pause
rem Check if we need to sign the application
rem if .%SIGN_APP%.==.0. goto RenamingFile
rem *************************************************************************
rem ** Sign the application
rem *************************************************************************
echo Signing...
echo Signing... >> "%ERRORS_FILE%"
call :CallSST

:RenamingFile
rem pause
echo Renaming binary file...
echo Renaming binary file... >> "%ERRORS_FILE%"
rem *************************************************************************
rem ** Copying File
rem *************************************************************************
copy %BIN_FILE% %SGN_FILE%
copy %TXT_FILE% %ADF_FILE%

if EXIST %M31_FILE% @del %M31_FILE%

echo ;%APP_NAME% >> "%M31_FILE%"
echo %ADF_FILE_NAME% >> "%M31_FILE%"
echo %SGN_FILE_NAME% >> "%M31_FILE%"

pause
goto End

rem *************************************************************************
rem ** End
rem *************************************************************************
:End
rem start %ERRORS_FILE%
goto Exit

rem *************************************************************************
rem ** Call the compiler
rem **  %1 is the directory
rem **  %2 is the file name without extention
rem *************************************************************************
:CallCompiler
echo %1\%2.c
echo %1\%2.c >> %ERRORS_FILE%
"%GNU_DIR%\bin\arm-elf-gcc" %CC_FLAGS% %CC_INCLUDES_DIR% %CC_DEFINES% "%1\%2.c" -o "%OBJ_DIR%\%2.o" 2>> "%ERRORS_FILE%"
if not ERRORLEVEL 1 goto Exit
set CC_ERROR=1
goto Exit

rem *************************************************************************
rem ** Call the linker
rem *************************************************************************
:CallLinker
"%GNU_DIR%\bin\arm-elf-ld" %LD_FLAGS% %LD_OBJECTS% %LD_SYS_OBJECTS% %LD_SYS_LIBS% %LD_GNU_LIBS% -o "%AXF_FILE%" 2>> "%ERRORS_FILE%"
if ERRORLEVEL 1 goto LinkError
echo.>> "%MAP_FILE%"
echo.>> "%MAP_FILE%"
echo Sizes (total data = data + bss)>> "%MAP_FILE%"
echo ===============================>> "%MAP_FILE%"
"%GNU_DIR%\bin\arm-elf-size" "%AXF_FILE%" >> "%MAP_FILE%"
goto Exit

:LinkError
set LD_ERROR=1
goto Exit

rem *************************************************************************
rem ** Call fromelf to generate the binary file
rem *************************************************************************
:CallFromElf
"%GNU_DIR%\bin\arm-elf-objcopy" --output-target=binary "%AXF_FILE%" "%BIN_FILE%" 2>> "%ERRORS_FILE%"
if not ERRORLEVEL 1 goto Exit
set FROMELF_ERROR=1
goto Exit

rem *************************************************************************
rem ** Sign the application
rem *************************************************************************
:CallSST
del "%SST_CONFIG_FILE%" > NUL 2> NUL

echo [SST]>> "%SST_CONFIG_FILE%"
echo ReaderName=>> "%SST_CONFIG_FILE%"
echo CertificateSchemaDir=%SST_DIR%\certificate>> "%SST_CONFIG_FILE%"
echo CertificateAppliDir=%SST_DIR%\certificate>> "%SST_CONFIG_FILE%"
echo SchemaDir=%SCHEMES_DIR%>> "%SST_CONFIG_FILE%"
echo SignedSchemaDir=%OBJ_DIR%>> "%SST_CONFIG_FILE%"
echo ParametersDir=%OBJ_DIR%>> "%SST_CONFIG_FILE%"
echo DestinationDir=%OUTPUT_DIR%>> "%SST_CONFIG_FILE%"
echo ApplicationDir=%OBJ_DIR%>> "%SST_CONFIG_FILE%"
echo TempDir=%SST_DIR%\Temp>> "%SST_CONFIG_FILE%"
echo.>> "%SST_CONFIG_FILE%"

echo [Schemas]>> "%SST_CONFIG_FILE%"
echo NbSchemas=%NUMBER_OF_SCHEMES% >> "%SST_CONFIG_FILE%"
echo CertificateFile=%SST_CERTIFICATE_FILE%>> "%SST_CONFIG_FILE%"

echo Schema1=%SCHEME1%>> "%SST_CONFIG_FILE%"
echo Schema2=%SCHEME2%>> "%SST_CONFIG_FILE%"
echo Schema3=%SCHEME3%>> "%SST_CONFIG_FILE%"
echo Schema4=%SCHEME4%>> "%SST_CONFIG_FILE%"
echo Schema5=%SCHEME5%>> "%SST_CONFIG_FILE%"
echo Schema6=%SCHEME6%>> "%SST_CONFIG_FILE%"
echo Schema7=%SCHEME7%>> "%SST_CONFIG_FILE%"
echo Schema8=%SCHEME8%>> "%SST_CONFIG_FILE%"
echo Schema9=%SCHEME9%>> "%SST_CONFIG_FILE%"
echo Schema10=%SCHEME10%>> "%SST_CONFIG_FILE%"
echo.>> "%SST_CONFIG_FILE%"

echo [Application]>> "%SST_CONFIG_FILE%"
echo ParameterFile=%TXT_FILE_NAME%>> "%SST_CONFIG_FILE%"
echo CertificateFile=%SST_CERTIFICATE_FILE%>> "%SST_CONFIG_FILE%"
echo ApplicationFile=%BIN_FILE_NAME%>> "%SST_CONFIG_FILE%"
echo CatalogFileExtension=%CATALOG_FILE%>> "%SST_CONFIG_FILE%"

"%SST_DIR%\SST30.exe" 2,%SST_CONFIG_FILE%,%SST_PIN%> NUL 2> NUL
rem del "%SST_CONFIG_FILE%" > NUL 2> NUL


goto Exit

rem *************************************************************************
rem ** Exit
rem *************************************************************************
:Exit
